﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Face_Recognition
{
    class Texel
    {
        int[, ,] matrix;
        int dimension;
        public Texel(int _dimension, Bitmap bitmap, int[] start)
        {
            dimension = _dimension;
            matrix = new int[dimension, dimension, 3];

            for(int i=0; i < dimension ; i++)
            {
                for (int j = 0; j < dimension; j++)
                {
                    matrix[i, j, 0] = bitmap.GetPixel(i + start[0], j + start[1]).R;
                    matrix[i, j, 1] = bitmap.GetPixel(i + start[0], j + start[1]).G;
                    matrix[i, j, 2] = bitmap.GetPixel(i + start[0], j + start[1]).B;                   
                }
            }
        }

        public bool checkTexture()
        {
            double[] dev = computeDeviation();

            if (dev[0] > 10 && dev[0] < 80) //moronic values
                return true;
            if (dev[1] > 10 && dev[1] < 80)
                return true;
            if (dev[2] > 10 && dev[2] < 80)
                return true;

            //if (dev[0] > 20 && dev[0] < 70)
            //    return true;
            //if (dev[1] > 30 && dev[1] < 70)
            //    return true;
            //if (dev[2] > 30 && dev[2] < 70)
            //    return true;

            return false;
        }

        private double[] computeMean()
        {
            double meanR=0, meanG=0, meanB=0;

            for (int i = 0; i < dimension; i++)
            {
                for (int j = 0; j < dimension; j++)
                {
                    meanR += matrix[i, j, 0];
                    meanG += matrix[i, j, 0];
                    meanB += matrix[i, j, 0];
                }
            }

            return new double[3] { meanR /= Math.Pow(dimension, 2), meanG /= Math.Pow(dimension, 2), meanB /= Math.Pow(dimension, 2) };
        }

        private double[] computeDeviation()
        {
            double[] means = computeMean();
            double[] difference = new double[3];

            for (int i = 0; i < dimension; i++)
            {
                for (int j = 0; j < dimension; j++)
                {
                    difference[0] += Math.Pow(matrix[i, j, 0] - means[0], 2);
                    difference[1] += Math.Pow(matrix[i, j, 1] - means[1], 2);
                    difference[2] += Math.Pow(matrix[i, j, 2] - means[2], 2);
                }
            }

            return new double[3] { Math.Sqrt(difference[0] / Math.Pow(dimension, 2)), Math.Sqrt(difference[1] / Math.Pow(dimension, 2)), Math.Sqrt(difference[2] / Math.Pow(dimension, 2)) };
        }
    }
}
